package com.yh.config;

import com.yh.config.properties.ProducerProperties;
import lombok.AllArgsConstructor;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author yh
 */
@Configuration
@AllArgsConstructor
public class RocketMqConfig {

    private final ProducerProperties producerProperties;

    @Bean
    public DefaultMQProducer defaultProducer() throws MQClientException {
        DefaultMQProducer producer = new DefaultMQProducer(producerProperties.getGroupName());
        producer.setNamesrvAddr(producerProperties.getServerAddress());
        producer.setMaxMessageSize(producerProperties.getMaxMessageSize());
        producer.setRetryTimesWhenSendAsyncFailed(producerProperties.getRetryTimesWhenSendFailed());
        producer.setSendMsgTimeout(producerProperties.getSendMsgTimeOut());
        producer.start();
        return producer;
    }

}
